$(function() {
	//点击跳转页面
	$(".news").click(function() {
		window.location.href = "../Notice/index.html";
	});
	$(".one").click(function() {
		window.location.href = "my_prize.html";
	});
	$(".two").click(function() {
		window.location.href = "page_duobaojilu.html";
	});
	$(".three").click(function() {
		window.location.href = "winning_record.html";
	});
	$(".four").click(function() {
		window.location.href = "page_duobaoxuzhi.html";
	});

	//判断是否有信息，显示条数
	$.ajax({
		type: "post",
		url: "http://a.91jfk.com/Product/Shopindex/ControlRedCircle",
		dataType: 'json',
		success: function(res) {
			if(res.code == 1 && (res.total - 0) > 0) {
				$(".news_red").css("display", 'block');
				$(".news_red").text(res.total + "+");
			} else if(res.code == 0) {
				$(".news_red").css("display", 'none');
			}
		},
		error: function(err) {
			console.log(err)
		}
	});

	//第一次进入页面时，默认类型是人气
	var type = "popularity";
	//展示商品
	ajax();
	//点击四个选项中的某一个，进行展示商品
	var list = $(".type");
	for(var i = 0; i < list.length; i++) {
		list.eq(i).click(function() {
			//加载前要先清除，不然就会累计加载
			$(".shop_list").html('');
			if($(this).text() == "人气") {
				type = "popularity";
			} else if($(this).text() == "最新") {
				type = "newest";
			} else if($(this).text() == "进度") {
				type = "schedule ";
			} else if($(this).text() == "高价") {
				type = "overbid";
			}
			$(this).addClass("color").siblings().removeClass("color");
			//展示商品
			ajax();

		});
	}

	//将ajax封装在一个方法中
	function ajax() {
		$.ajax({
			type: "post",
			url: "http://a.91jfk.com/Product/Indiana/index",
			data: {
				platform: 1,
				state: type
			},
			dataType: 'json',
			success: function(res) {
				console.log(res);
				if(res.code == "1") {
					var data = res.msg;
					for(var i = 0; i < data.length; i++) {
						showItem(data[i].thumbnail, data[i].pname, data[i].price, data[i].id);
					}
				}
			},
			error: function(err) {
				console.log(err)
			}
		});
	}

	function showItem(img, name, price, id) {
		var shop_list = $(".shop_list");
		var shop_one = $('<div class="shop_one"><img style="width: 3.32rem;margin-top: 0.666666rem;margin-left: 0.52rem;" src= "' + img + '"/><p class="name">' + name + '</p><p class="immediately">立即夺宝<span style="display:none">&yen;' + price + '集分</span></p><p class="id" style = "display:none">' + id + '</p></div>');
		shop_list.append(shop_one);
	}

	//点击每个商品进入详情页
	$(".shop_list").on("click", ".shop_one", function() {
		var id = $(this).find(".id").text();
		var storage = window.localStorage;
		storage.setItem("pid", id);
		window.location.href = "page_dbDetail.html";
	});
	
	
	$(".rake img").click(function(){
		var keywords = $(this).parent().find("input").val();
		var storage = window.localStorage;
		storage.setItem("Indiana_word",keywords);
		window.location.href ="Indiana_list.html";
	});
	
	
	//显示广播的信息
		$.ajax({
			type: "post",
			url: "http://a.91jfk.com/Product/Order/airing",
			data: {
			},
			dataType: 'json',
			success: function(res) {
				console.log(res);
				for(var i = 0; i < res.length;i++){
					var time = format(res[i].create_time - 0);
					var now_time = format(Date.parse(new Date()));
					var EndTime= new Date(now_time);
    				var NowTime = new Date(time);
    				var t =EndTime.getTime() - NowTime.getTime();
    				var m=Math.floor(t/1000/60);
					show_broadcast(res[i].nickname,m,res[i].pname);
				}
			},
			error: function(err) {
				console.log(err)
			}
		});
		
	function show_broadcast(name,time,pname){
		var broadcast = $(".guangbo ul");
		var oli = $('<li>恭喜<span>' + name + '</span>在' + time +'分钟前获得<span>' + pname + '</span></li>');
		broadcast.append(oli);
	}
	//将时间戳转化为具体时间
	function add0(m) {
		return m < 10 ? '0' + m : m
	}
	function format(shijianchuo) {
		//shijianchuo是整数，否则要parseInt转换
		var time = new Date(shijianchuo);
		var y = time.getFullYear();
		var m = time.getMonth() + 1;
		var d = time.getDate();
		var h = time.getHours();
		var mm = time.getMinutes();
		var s = time.getSeconds();
		return y + '-' + add0(m) + '-' + add0(d) + ' ' + add0(h) + ':' + add0(mm) + ':' + add0(s);
	}
  
  
  //广播的移动
  var $this = $(".guangbo");
		var scrollTimer;
		$this.hover(function() {
			clearInterval(scrollTimer);
		}, function() {
			scrollTimer = setInterval(function() {
				scrollNews($this);
			}, 3000);
		}).trigger("mouseleave");

		function scrollNews(obj) {
			var $self = obj.find("ul");
			var lineHeight = $self.find("li:first").height();
			$self.animate({
				"marginTop": -lineHeight + "px"
			}, 1000, function() {
				$self.css({
					marginTop: 0
				}).find("li:first").appendTo($self);
			})
		}
	

});